<template>
    <el-form :model="orderinfo" label-width="70px" style="padding: 10px; height: 100%;display: flex;flex-direction: column;box-sizing: border-box;">
            <el-form-item label="选择产品" style="height: 100%;">
                <el-button type="success" @click="getInfo('/selectGoods')" icon="h-kaidan" class="mb">选择产品</el-button>
                <el-table :data="list" :stripe="true" height="500" show-summary>
                    <el-table-column prop="id" label="ID" width="80"></el-table-column>
                    <el-table-column prop="name" label="商品名称"></el-table-column>
                    <el-table-column prop="num" label="数量" width="100">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.num" size="mini"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="移除" width="150">
                        <template slot-scope="scope">
                            <el-button icon="delete2" type="danger" size="small" @click="deleteInfo(scope.$index)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form-item>
            <el-form-item label="描述信息">
                <el-input type="textarea" v-model="orderinfo.remarks" placeholder="请输入出入库描述信息"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="upload2" @click="onSubmit">保存提交</el-button>
                <el-button type="danger" icon="close" @click="closewin">取消关闭</el-button>
            </el-form-item>
        </el-form>
</template>
<script type="text/javascript">
import C from '#/src/common/common.js'

export default {
    name: 'addStockOrder',
    data() {
        return {
            list: [], // 商品列表
            orderinfo: {
                remarks: '',
                ordertype: C.getDatas().ordertype
            },
            alln: 0
        }
    },
    methods: {
        getInfo: async function(url) {
            var that = this
            let data = await C.openBrowserWindow(url, {}, { width: 1200, height: 880, useSpa: true, spaModel: 'shop', transparent: true, frame: false })
            if (data) {
                for (var i = data.length - 1; i >= 0; i--) {
                    if (!this.inarray(data[i].id, that.list)) {
                        let cacheData = {
                            id: data[i].id,
                            name: data[i].name,
                            num: 1
                        }
                        that.list.push(cacheData)
                    }
                }
            }
        },
        deleteInfo: function(index) {
            this.list.splice(index, 1)
        },
        onSubmit: async function() {
            let goback = false
            let len = this.list.length
            if (len < 1) {
                C.error('请添加' + this.typename + '产品！')
                return false
            }
            for (let i = 0; i < len; i++) {
                if (this.list[i].num <= 0) {
                    goback = true
                }
            }
            if (goback) {
                C.error(this.typename + '产品数量不能小于或等于零！')
                return false
            }
            let orderinfo = {
                totalnum: this.totalnum
            }
            Object.assign(orderinfo, this.orderinfo)
            let data = {
                list: this.list,
                orderinfo: orderinfo
            }
            let res = await C.ajax('/shop/stock/addstockorder', { data: JSON.stringify(data) }, 'post')
            if (res) C.closeBrowserWindow(res)
        },
        closewin: function() {
            C.closewin()
        },
        inarray(id, array) {
            let res = false
            for (var i = array.length - 1; i >= 0; i--) {
                if (id == array[i].id) {
                    res = true
                }
            }
            return res
        }
    },
    computed: {
        typename: function() {
            return this.orderinfo.ordertype == 6 ? '入库' : '出库'
        },
        totalnum: function() {
            let totalnum = 0
            this.list.forEach((row) => {
                totalnum += Number(row.num)
            })
            return totalnum
        }
    }
}
</script>